<?php

namespace app\admin\controller\mall\oper;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 帮助分类
 *
 * @icon fa fa-circle-o
 */
class Helpcat extends Backend
{
    
    /**
     * Helpcat模型对象
     * @var \app\common\model\mall\oper\Helpcat
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\oper\Helpcat;
        $this->view->assign("typeList", $this->model->getTypeList());
        
        // 必须将结果集转换为数组
        $catList = collection($this->model->order('pid,weigh', 'asc')->select())->toArray();
        //生成分类树
        Tree::instance()->init($catList);
        //生成权节点的字段为'name'
        $this->catlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        //生成父节点数据，用于表单选择器
        $catdata = [0 => __('None')];
        foreach ($this->catlist as &$v)
        {
        	if (!$v['showis']) continue;
        	$catdata[$v['id']] = $v['name'];
        }
        $this->view->assign('catdata', $catdata);
        
    }


    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {

            $total = count($this->catlist);

            $result = array("total" => $total, "rows" => $this->catlist);

            return json($result);
        }
        return $this->view->fetch();
    }
}
